Providing a Flex Front End to Your Struts Applications
Table of Contents
There are two strategies for exposing Struts components (or their façade) as services:
- You can expose the components as web services.
- You can use Flex's RemoteObject to invoke methods directly on Struts components (or their Façade). Notice that behind the scenes RemoteObject also handles your remote method invocation using SOAP.
The strategy you choose is really an implementation detail. Using web services is a more general approach and gives you more flexibility. For example, the Flex application doesn't have to be on the same machine as the Struts application. Using RemoteObject might be a simpler approach if your Flex and your Struts applications live on the same machine and you don't want to go through the extra effort of exposing components as web services.
The source code below is the web services strategy. The Flex client application interoperates with the RegistrationService exposed as a web service.
<?xml version="1.0" encoding="iso-8859-1"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" height="600">
<mx:WebService id="registrationWS" wsdl="http://localhost:8101/royale/services/Registration?wsdl"
result="alert(registrationWS.register.result==null?'Thank You!':'Sorry, your registration failed')">
<mx:operation name="register">
<mx:request>
<form>{registrationModel}</form>
</mx:request>
</mx:operation>
</mx:WebService>
<mx:Model id="registrationModel">
<firstName>{firstName.text}</firstName>
<lastName>{lastName.text}</lastName>
<phone>{phone.text}</phone>
<email>{email.text}</email>
</mx:Model>
<mx:Form>
<mx:FormItem label="First Name" required="true">
<mx:TextInput id="firstName" width="200"/>
</mx:FormItem>
<mx:FormItem label="Last Name" required="true">
<mx:TextInput id="lastName" width="200"/>
</mx:FormItem>
<mx:FormItem label="Phone" required="true">
<mx:TextInput id="phone" width="200"/>
</mx:FormItem>
<mx:FormItem label="Email" required="true">
<mx:TextInput id="email" width="200"/>
</mx:FormItem>
<mx:FormItem>
<mx:Button label="Register" click="registrationWS.register.send()"/>
</mx:FormItem>
</mx:Form>
<mx:Repeater id="errorList" dataProvider="{registrationWS.register.result.item}">
<mx:Label>{errorList.currentItem}</mx:Label>
</mx:Repeater>
</mx:Application>
Code Highlights
- The
<mx:Model>tag represents the data. - Data elements in the
<mx:Model>tag are bound to the data input controls: When the user makes changes in user interface controls, the<mx:Model>tag automatically reflects the changes. - The input parameter of the web service's register method is bound to the registrationModel: When you invoke the method, it passes the registrationModel automatically as an argument.
Behind the scenes, registrationModel is represented by an ActionScript
object. The properties of this object match the properties of the server-side
RegistrationForm JavaBean. You can therefore use registrationModel
as the input parameter for the register() method in the
RegistrationService class exposed as a web service: Flex serializes
registrationModel into a SOAP request on the client side and deserializes
the SOAP packet into an instance of the RegistrationForm JavaBean at
the server side.
Alternative Implementation
Instead of using the <mx:Model> tag to represent
the data, you can also use your own ActionScript object. For example,
you could create a RegistrationForm.as class as follows:
class RegistrationForm {
var firstName:String;
var lastName:String;
var phone:String;
var email:String;
function RegistrationForm(firstName:String, lastName:String, phone:String, email:String) {
this.firstName=firstName;
this.lastName=lastName;
this.phone=phone;
this.email=email;
}
}
You can then use this class instead of the <mx:Model> tag
to represent the registration data on the client side:
<?xml version="1.0" encoding="iso-8859-1"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" height="600">
<mx:Script>
var form;
function register() {
form = new RegistrationForm(firstName.text, lastName.text, phone.text, email.text);
registrationWS.register.send();
}
</mx:Script>
<mx:WebService id="registrationWS" wsdl="http://localhost:8101/royale/services/Registration?wsdl"
result="alert(registrationWS.register.result==null?'Thank You!':'Sorry, your registration failed')">
<mx:operation name="register">
<mx:request>
<form>{form}</form>
</mx:request>
</mx:operation>
</mx:WebService>
<mx:Form>
<mx:FormItem label="First Name" required="true">
<mx:TextInput id="firstName" width="200"/>
</mx:FormItem>
<mx:FormItem label="Last Name" required="true">
<mx:TextInput id="lastName" width="200"/>
</mx:FormItem>
<mx:FormItem label="Phone" required="true">
<mx:TextInput id="phone" width="200"/>
</mx:FormItem>
<mx:FormItem label="Email" required="true">
<mx:TextInput id="email" width="200"/>
</mx:FormItem>
<mx:FormItem>
<mx:Button label="Register" click="register()"/>
</mx:FormItem>
</mx:Form>
<mx:Repeater id="errorList" dataProvider="{registrationWS.register.result.item}">
<mx:Label>{errorList.currentItem}</mx:Label>
</mx:Repeater>
</mx:Application>
Conclusion
Although Struts greatly facilitates the task of developers building web applications based on Java, it doesn't attempt to improve the end-user experience. Macromedia Flex addresses this requirement by providing a platform for building Rich Internet Applications. Because Flex and Struts are both based on a Model-View-Controller architecture but focus on different aspects of this architecture, the integration of these two technologies makes a lot of sense. You can implement the Flex/Struts integration using the HTTP request/response mechanism maintaining Struts' traditional workflow, or using a service-oriented architecture (SOA), transforming Struts artifacts into services.